//载入模块儿

const express = require("express");

const router = express.Router();

const path = require("path");

const fs = require("fs");
const { parse } = require("path");

const timestamp = require("time-stamp");
const { timeStamp } = require("console");
router.get("/register",(req,res)=>{
    res.sendFile(path.join(__dirname,"register.html"));
})
router.post("/toregister",(req,res)=>{
    req.curtime = timeStamp("YYYY-MM-DD HH:mm:ss");
    let {tel,pass,pass2} = req.body;
    let content = fs.readFileSync(path.join(__dirname,"../person.json"));
    let userinfo  = JSON.parse(content.toString());
    if(tel==""||pass==""||pass2 =="")
    {
        res.send("<script>alert('用户名或者密码不得为空');location.href='/register';</script>")
        return;
    }
    else if(pass != pass2)
    {
        res.send("<script>alert('两次输入密码不一致');location.href='/register';</script>")
        return;
    }
    else
    {

      let obj = {};
      obj.username = tel;
      obj.password = pass;
      obj.time = req.curtime;
      userinfo.push(obj);
      fs.writeFileSync(path.join(__dirname,"../person.json"),JSON.stringify(userinfo),{flag:"w"});
      res.send("<script>alert('注册成功');location.href='/login'</script>");
    }
})


module.exports = router;